package com.yzt.zhuju.utils;

import android.content.Context;
import android.content.res.AssetManager;

import androidx.fragment.app.FragmentActivity;

import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.constants.PreferenceConstant;
import com.yzt.zhuju.network.entity.sysConfig.CountryBean;
import com.yzt.p_base.utils.SharedPreferenceUtil;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/13
 * desc   : 国家实体类
 */
public class CountryJsonUtil {

    public static List<String> getCountryJsonList(FragmentActivity activity) {

        List<String> listData = new ArrayList<>();

        String json = SharedPreferenceUtil.getInstance().getValue(PreferenceConstant.SYSTEM_NATIONAL_LIST, "");

        if ("".equals(json)) {
            return listData;
        }

        List<CountryBean> list = JSONObject.parseArray(json, CountryBean.class);
        for (CountryBean bean : list) {
            listData.add(bean.getName());
        }
        return listData;
    }

    public static List<String> getLanguageJsonList(FragmentActivity activity) {

        List<String> listData = new ArrayList<>();

        String json = SharedPreferenceUtil.getInstance().getValue(PreferenceConstant.SYSTEM_LANGUAGE_LIST, "");

        if ("".equals(json)) {
            return listData;
        }

        List<CountryBean> list = JSONObject.parseArray(json, CountryBean.class);
        for (CountryBean bean : list) {
            listData.add(bean.getLanguage());
        }
        return listData;
    }

    public static String formatLanguageId(String language) {

        String id = "";

        String json = SharedPreferenceUtil.getInstance().getValue(PreferenceConstant.SYSTEM_LANGUAGE_LIST, "");

        if ("".equals(json)) {
            return id;
        }

        List<CountryBean> list = JSONObject.parseArray(json, CountryBean.class);
        for (CountryBean bean : list) {
            if (bean.getLanguage().equals(language)) {
                return bean.getId();
            }
        }
        return id;
    }

    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}
